package com.example.testspringes.controller;

import com.example.testspringes.jiehun.Test;
import com.example.testspringes.modul.GeneticTestingPackage;
import com.example.testspringes.modul.Person;
import com.example.testspringes.service.EsService;
import com.example.testspringes.threadtest.Server;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.rest.RestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: //TODO
 * @ProjectName: testspringes
 * @Package: com.example.testspringes.controller
 * @Author: FX
 * @CreateDate: 2018/11/19 16:08
 * @Version: 1.0
 * Copyright: Copyright (c) 2018
 */
@RequestMapping("test")
@RestController
public class TestController {
    @Autowired
    EsService esService;
    @Resource
    RestTemplate restTemplate;
    @GetMapping("index")
    public String g(){
        return "index2";
    }

    @PostMapping
    public Map<String,Object> add(@RequestBody GeneticTestingPackage geneticTestingPackage){
        RestStatus add = esService.add(geneticTestingPackage);
        Map<String,Object> map = new HashMap<>(1);
        map.put("data",add.getStatus());
        return map;
    }

    @GetMapping
    public Map<String,Object> get(){
        //id查询
        Map<String,Object> map = new HashMap<>(1);
        Map<String, Object> sourceAsMap = esService.get();
        map.put("data",sourceAsMap);
        //List<GeneticTestingPackage> geneticTestingPackages = packageDao.selectAll();
        //System.out.println(geneticTestingPackages);
        return map;
    }
    @GetMapping("huangli")
    public Map<String,Object> get2(String date){
        if(StringUtils.isEmpty(date)){
            date="2019-03-06";
        }
        String url="http://v.juhe.cn/laohuangli/d?date="+date+"&key=c973ddcc0f55d9f9596c912acf1c4b07";
        ResponseEntity<Map> forEntity = restTemplate.getForEntity(url, Map.class);

        return forEntity.getBody();
    }

    @Autowired
    Server server;
    //@Autowired
    //Test test;
    @GetMapping("json")
    public Person ge2(){
        Person person = new Person();
        person.setBirth("a");
        System.out.println(person.getBirth());
        person.setDate(System.currentTimeMillis());
        //server.start();
       // test.start();

        return person;
    }
}
